package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.entity.PageResult;
import com.pinyougou.entity.Result;
import com.pinyougou.pojo.TypeTemplate;
import com.pinyougou.sellergoods.service.TypeTemplateService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("typeTemplate")
public class TypeTemplateController {
    @Reference
    private TypeTemplateService typeTemplateService;

    @GetMapping("{id}")
    public TypeTemplate detail(@PathVariable Integer id) {
        TypeTemplate typeTemplate = typeTemplateService.findById(id);
        return typeTemplate;
    }

    @GetMapping("list")
    public List<TypeTemplate> findAll() {
        List<TypeTemplate> typeTemplateList = typeTemplateService.findAll();
        return typeTemplateList;
    }

    @GetMapping("list/{pageNum}/{pageSize}")
    public PageResult<TypeTemplate> findAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageResult<TypeTemplate> pageResult = typeTemplateService.findByPage(pageNum, pageSize);
        return pageResult;
    }

    @PutMapping("search/{pageNum}/{pageSize}")
    public PageResult<TypeTemplate> search(@RequestBody(required = false) TypeTemplate typeTemplate,
                                           @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageResult<TypeTemplate> pageResult = typeTemplateService.findByPage(typeTemplate, pageNum, pageSize);
        return pageResult;
    }

    @PostMapping
    public Result add(@RequestBody TypeTemplate typeTemplate) {
        try {
            System.out.println(typeTemplate);
            typeTemplateService.save(typeTemplate);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody TypeTemplate typeTemplate) {
        try {
            typeTemplateService.update(typeTemplate);
            return new Result(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败");
        }
    }

    @DeleteMapping("delete")
    public Result delete(Integer[] ids) {
        if (ids == null || ids.length < 1)
            return new Result(false, "必须选中一个");
        try {
            typeTemplateService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }
}
